﻿using Bangle.SerialPort.Messages;
using System.Threading.Tasks;

namespace Bangle.SerialPort
{
    public class SerialPortSender : ISerialPortSender
    {
        private readonly SerialPortFactory _factory;
        public SerialPortSender(SerialPortFactory factory)
        {
            _factory = factory;
        }

        public Task SendAsync(string name, byte[] buffer)
        {
            var serialPort = _factory.GetSerialPort(name);
            serialPort.Write(buffer, 0, buffer.Length);
            return Task.CompletedTask;
        }

        public Task SendAsync(string name, ISendMessage msg)
        {
            return SendAsync(name, msg.Serialize());
        }
    }
}
